﻿using System;
using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using WindowsPhoneRssReader.Models;

namespace WindowsPhoneRssReader
{
    public partial class FeedItemDetailsPage
    {


        /*
         * set navigator
         * 1: go home
         * 2: go to feed
         * go back
         */
        private short navigator;
        public FeedItemDetailsPage()
        {
            InitializeComponent();
            PageTransitionDetails.Completed += PageTransitionDetails_Completed;
            Loaded += (sender, e) => LoadStory();
        }

        void LoadStory()
        {
            var feedItem = DataContext as FeedItem;
            if (feedItem == null) return;
            feedItem.Read = true;
            UpdateButttonEnable();
        }

        private void UpdateButttonEnable()
        {
            var feedItem = DataContext as FeedItem;
            if (feedItem == null) return;
            var feed = feedItem.ParentFeed;
            var selectedIndex = feed.FeedItems.IndexOf(feedItem);
            ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = selectedIndex > 0;
            ((ApplicationBarIconButton)ApplicationBar.Buttons[3]).IsEnabled = selectedIndex < feed.FeedItems.Count - 1;
            scrollViewer.ScrollToVerticalOffset(0);
        }

        void PageTransitionDetails_Completed(object sender, EventArgs e)
        {
            switch (navigator)
            {
                case 1:
                    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                    break;
                case 2:
                    NavigationService.Navigate(new Uri("/FeedDetailsPage.xaml", UriKind.Relative));
                    var root = Application.Current.RootVisual as FrameworkElement;
                    if (root == null) return;
                    var feedItem = DataContext as FeedItem;
                    if (feedItem != null) root.DataContext = feedItem.ParentFeed;
                    break;
                default:
                    //reset root frame MainPage.xaml
                    NavigationService.GoBack();
                    break;
            }

        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // Cancel default navigator
            e.Cancel = true;
            // do work animation
            PageTransitionDetails.Begin();
        }

        private void NextItem(object sender, EventArgs e)
        {
            var feedItem = DataContext as FeedItem;
            if (feedItem == null) return;

            var feed = feedItem.ParentFeed;
            var index = feed.FeedItems.IndexOf(feedItem);
            index = Math.Min(feed.FeedItems.Count - 1, index + 1);
            feed.CurrentFeedItem = feed.FeedItems[index];

            DataContext = feed.CurrentFeedItem;
            LoadStory();
        }

        private void PreviousItem(object sender, EventArgs e)
        {

            var feedItem = DataContext as FeedItem;
            if (feedItem == null) return;

            var feed = feedItem.ParentFeed;
            var index = feed.FeedItems.IndexOf(feedItem);
            index = Math.Max(0, index - 1);
            feed.CurrentFeedItem = feed.FeedItems[index];

            DataContext = feed.CurrentFeedItem;
            LoadStory();
        }

        private void SetFavouriteItem(object sender, EventArgs e)
        {
            var feedItem = DataContext as FeedItem;
            if (feedItem == null) return;
            feedItem.Favourite = !feedItem.Favourite;
        }

        private void GoHome(object sender, EventArgs e)
        {
            //NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            navigator = 1;
            PageTransitionDetails.Begin();

        }

        private void ViewInBrowser(object sender, EventArgs e)
        {
            var feedItem = DataContext as FeedItem;
            if (feedItem == null) return;
            var webTask = new WebBrowserTask { Uri = new Uri(feedItem.Url, UriKind.Absolute) };
            webTask.Show();
        }


        private void GoToFeed(object sender, EventArgs e)
        {
            navigator = 2;
            PageTransitionDetails.Begin();
        }

        private void MarkUnRead(object sender, EventArgs e)
        {
            var feedItem = DataContext as FeedItem;
            if (feedItem == null) return;
            feedItem.Read = false;
        }
    }
}